# coding: utf-8 or # -*- coding: utf-8 -*-

import os
import json


from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy import func
from sqlalchemy import Table, Column, Integer, String, Text, DateTime, MetaData, ForeignKey, Boolean, types, Numeric
from datetime import datetime, date
import hashlib
from sqlca import get_Session
from encrpty import EncrptyUtil

os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'

Base = declarative_base()

class infor_pos(Base):
    # 表的名字:
    __tablename__ = 'infor_pos'
    # 表的结构:
    C_POS_ID = Column(String(30), primary_key=True)
    C_SHOP_ID = Column(String(10))
    C_POS_PLACE = Column(String(10))
    C_POS_TELEPHONE = Column(String(20))
    IP_ADDR = Column(String(20))
    SERVERIP = Column(String(20))
    CARDTYPE = Column(String(20))
    CFLOOR = Column(String(20))
    CWORK_DIR = Column(String(20))
    CNEWFLAG = Column(String(20))
    HEAD1 = Column(String(40))
    HEAD2 = Column(String(40))
    HEAD3 = Column(String(40))
    HEAD4 = Column(String(40))
    TAIL1 = Column(String(40))
    TAIL2 = Column(String(40))
    TAIL3 = Column(String(40))
    TAIL4 = Column(String(40))
    CPARA = Column(String(40))
    PRECASH_CTRL = Column(String(40))
    PRECASH_LOWER = Column(String(40))
    PRECASH_UPER = Column(String(40))
    DRAWER_CTRL = Column(String(40))
    DRAWER_TIME = Column(String(40))
    KC_CTRL = Column(String(40))
    LOCK_IDLE = Column(String(40))
    WEB_SITE = Column(String(40))
    SKIN = Column(String(40))
    SKIN_NAME = Column(String(40))
    UPD_FLAG = Column(String(40))
    UPD_UPD_TIME = Column(String(40))
    POS_DISKEY = Column(String(40))
    POS_DUMP = Column(String(40))
    POS_BARCODE = Column(String(40))
    POS_SHUTDOWN = Column(String(40))
    PRN_TYPE = Column(String(40))
    PRN_PORT = Column(String(40))
    PRN_CUT_PAPER = Column(String(40))
    PRN_FEED_PAPER = Column(String(40))
    PRN_INCH_PAPER = Column(String(40))
    PRN_BAUDRATE = Column(String(40))
    PRN_PARITY = Column(String(40))
    C_VALID_FLAG = Column(String(10))
    # PRN_BYTESIZE
    # PRN_STOPBITS
    # VFD_TYPE
    # VFD_PORT
    # VFD_BAUDRATE
    # VFD_PARITY
    # VFD_BYTESIZE
    # VFD_STOPBITS
    # VFD_LINENUM
    # VFD_LINELEN
    # VFD_LINENEEDENTER
    # VFD_INITCMD
    # VFD_CLSCMD
    # VFD_CMD1
    # VFD_CMD2
    # VFD_PRICECMD1
    # VFD_PRICECMD2
    # VFD_SUMCMD1
    # VFD_SUMCMD2
    # VFD_CHANGECMD1
    # VFD_CHANGECMD2
    # VFD_HELLOSTR1
    # VFD_HELLOSTR2
    # VFD_BYESTR1
    # VFD_BYESTR2
    # VFD_CLOSESTR1
    # VFD_CLOSESTR2
    # DRAWER_TYPE
    # MSR_TYPE
    # ARG_NEW_FLAG
    # C_COUNTER_ID


def pos_en(pos_no):
    pos_no = pos_no.strip()
    session = get_Session()()
    query = session.query(func.count(1)).filter(
        infor_pos.C_POS_ID == pos_no).scalar()
    if query == 0:
        ls_msg = u"终端%s不存在"%pos_no
        session.close()
        return {"code": "35", "error": ls_msg}
    else:
        query2 = session.query(infor_pos).filter(
        infor_pos.C_POS_ID == pos_no).scalar()
        shop_id = query2.C_SHOP_ID
        ls_msg = "succeed"
        session.close()
        return {"code": "10", "error": ls_msg,"C_SHOP_ID":shop_id}


def token_create(pos_no, operno):
    session = get_Session()()
    dt = session.execute(
        "select to_char(sysdate + 1,'yyyy-mm-ddhh:mm:ss') from DUAL").first()[0]
    ls_str = dt + ',' + pos_no + ',' + operno
    print ls_str
    #ls_token = EncrptyUtil().encrypt(ls_str)
    ls_token = ls_str
    session.close()
    return ls_token


def token_check(key):
    if len(key) <= 1:
        ls_msg = u"token无效,请重新登录1"
        return {"code": "35", "error": ls_msg}
    key2 = key.strip()
    key2 = unicode(key2)
    print '-------key-------------'
    print key
    print '-------key-------------'
    # key = '831<005027!0:;55;99-<26872#9732'

    #ls_key = EncrptyUtil().decrypt(key2)
    ls_key = key2
    print '======================'
    print ls_key
    print '======================'
    if ls_key.find(',') <= 0:
        ls_msg = u"token无效,请重新登录2"
        return {"code": "35", "error": ls_msg}
    li = ls_key.split(',')
    dt_old = li[0]
    pos_no = li[1]
    oper = li[2]
    # pos = li[1]
    session = get_Session()()
    dt = session.execute(
        "select to_char(sysdate,'yyyy-mm-ddhh:mm:ss') from DUAL").first()[0]
    if dt > dt_old:
        ls_msg = u"token无效,请重新登录3"
        session.close()
        return {"code": "35", "error": ls_msg}
    else:
        ls_msg = "succeed"
        session.close()
        return {"code": "10", "error": ls_msg,"dt":dt_old,"pos_no":pos_no,"operator":oper}


# lst = 'sdfsssdf'
# print EncrptyUtil().encrypt(lst)
# print token_create('99999', '33')
# print token_check('8559/11375#23:66>83-9?>=<.43')

# session = get_Session()()
# print session.execute("select to_char(sysdate,'yyyy-mm-dd hh:mm:ss')
# from DUAL").first()[0]

# print pos_en('99999')


# pos_no = '99999'
# query2 = session.query(infor_pos).filter(
#     infor_pos.C_POS_ID == pos_no).scalar()
# shop_id = query2.C_SHOP_ID
# print query2.C_SHOP_ID
